<template>
	<view class="container">
		<!-- <u-toast ref="uToast"></u-toast> -->
		<!-- 轮播图 -->
		<view class="carousel">
			<swiper indicator-dots circular=true duration="400" :autoplay="true"
					:interval="4000" :duration="500">
				<swiper-item class="swiper-item" v-for="(item,index) in imgList" :key="index">
					<view class="image-wrapper">
						<image :src="goods.goods_thumb" class="loaded" mode="aspectFill"></image>
					</view>
					
				</swiper-item>
			</swiper>
		</view>
	
		<!-- name,goods_thumb,goods_desc,goods_banner_imgs,price,total_sell_count,remain_count -->
		<view class="introduce-section">
			<text class="title">{{goods.goods_desc}}</text>
			<view class="price-box">
				<text class="price-tip" style="color: red;">¥</text>
				<text class="price">{{goods.remain_count}}</text>
				<!-- <text class="m-price">¥488</text> -->
				<!-- <text class="coupon-tip">7折</text> -->
			</view>
			<view class="bot-row">
				<text>销量: {{goods.total_sell_count}}</text>
				<text>库存: {{goods.total_sell_count}}</text>
				<!-- <text>浏览量: 768</text> -->
			</view>
		</view>

		<!--  分享 -->
		<view class="share-section">
			<view class="share-icon">
				<text class="yticon icon-xingxing"></text>
				返
			</view>
			<text class="tit">该商品分享可领49减10红包</text>
			<text class="yticon icon-bangzhu1"></text>
			<view class="share-btn">
				立即分享
				<!-- <text class="yticon icon-you"></text> -->
			</view>
		</view>

		<view class="c-list">
			<view class="c-row b-b" @click="toggleSpec">
				<text class="tit">购买类型</text>
				<view class="con">
					<text class="selected-text" v-for="(sItem, sIndex) in specSelected" :key="sIndex">
						{{sItem.name}}
					</text>
				</view>
				
				<text class="yticon icon-you"></text>
			</view>
			<view class="c-row b-b">
				<text class="tit">优惠券</text>
				<text class="con t-r red">领取优惠券</text>
				<text class="yticon icon-you"></text>
			</view>
				<!-- <uni-goods-nav @click="toggleSpec"></uni-goods-nav> -->
			<view class="c-row b-b">
				<text class="tit">促销活动</text>
				<view class="con-list">
					<text>新人首单送20元无门槛代金券</text>
					<text>订单满50减10</text>
					<text>订单满100减30</text>
					<text>单笔购买满两件免邮费</text>
				</view>
			</view>
		
			<view class="c-row b-b">
				<text class="tit">服务</text>
				<view class="bz-list con">
					<text>7天无理由退换货 ·</text>
					<text>假一赔十 ·</text>
				</view>
			</view>
		</view>

		
		
		<view class="eva-section" >
			<view class="e-header">
				<uni-segmented-control :current="current" :values="items" @clickItem="onClickItem" styleType="text"
					activeColor="#4cd964"></uni-segmented-control>
				<!-- <text class="tit">评价</text>
				<text>(999)</text> -->
				<!-- <text class="tip">好评率 100%</text> -->
				<text class="yticon icon-you"></text>
			</view>
			<view class="eva-box" v-show="current==0">
			
				<view class="detail-desc">
					
					<view style="width:450px;height: 450px;" v-for="(item,index) in imgList.slice(0,19)">
						<image style="max-width:100%; max-height: 100%; display:block;" :src="item"></image>
					</view>
				</view>
				
			</view>
			<view class="eva-box" v-show="current==1">
				
				<image class="portrait" src="/static/uni-center/grey.png" mode="aspectFill"></image>
				<view class="right">
					<text class="name">杰哥</text>
					<text
						class="con">太好用了</text>
					<view class="bot">
						<text class="attr">时间： 2019-04-01 19:21 </text>
						<text class="time"></text>
					</view>
				</view>
				
			</view>
			<view class="eva-box" v-show="current==2">
				<!-- <view class="abb">
					<view>商品品牌: {{goods.name}}</view>
				</view> -->
				<view class="abb">
					<view>商品品牌: {{goods.name}}</view>
					<view>搜索建议: {{goods.keywords}}</view>
					<view>商品详细: {{goods.goods_desc}}</view>
				</view>
				
			</view>
		
		
		
		</view>
		
		
		

		

		<!-- 底部操作菜单 -->
		<view class="page-bottom">
			<navigator url="/pages/grid/grid" open-type="switchTab" class="p-b-btn">
				<text class="yticon icon-xiatubiao--copy"></text>
				<text>首页</text>
			</navigator>
			<navigator url="/pages/list/gouwuche" open-type="switchTab" class="p-b-btn">
				<text class="yticon icon-gouwuche"></text>
				<text >购物车</text>
			</navigator>
			<view class="p-b-btn">
				<text class="yticon icon-shoucang"></text>
				<text>收藏</text>
			</view>

			<view class="action-btn-group">
				<button type="primary" class=" action-btn no-border buy-now-btn">立即购买</button>
				<button type="primary" class=" action-btn no-border add-cart-btn" @click="gouwoche">加入购物车</button>
			</view>
		</view>


		<!-- 规格-模态层弹窗 -->
		<view class="popup spec" :class="specClass" @touchmove.stop.prevent="stopPrevent" @click="toggleSpec">
			<!-- 遮罩层 -->
			<view class="mask"></view>
			<view class="layer attr-content" @click.stop="">
				<view class="a-t">
					<image :src="goods.goods_thumb"></image>
					<view class="right">
						<text class="price">¥{{goods.remain_count}}</text>
						<text class="stock">库存: {{goods.total_sell_count}}</text>
						<view class="selected">
							已选：
							<text class="selected-text" v-for="(sItem, sIndex) in specSelected" :key="sIndex">
								{{sItem.name}}
							</text>
						</view>
					</view>
				</view>
				<view v-for="(item,index) in specList" :key="index" class="attr-list">
					<text>{{item.name}}</text>
					<view class="item-list">
						<text v-for="(childItem, childIndex) in specChildList" v-if="childItem.pid === item.id"
							:key="childIndex" class="tit" :class="{selected: childItem.selected}"
							@click="selectSpec(childIndex, childItem.pid)">
							{{childItem.name}}
						</text>
					</view>
				</view>
				<button class="btn" @click="toggleSpec">完成</button>
			</view>
		</view>
	</view>
</template>

<script>
	const db = uniCloud.database();
	export default {
		data() {
			return {
				id: "",
				islogin:false,
				goods: "",
				mycart: [],
				specClass: 'none',
				items: ['图文详细', '评价(999)','详细参数' ],
				current: 0,
				contentid:"",
				specSelected: [],
				shareList: [],
				imgList: [],
				specList: [{
						id: 1,
						name: '版本',
					},
					{
						id: 2,
						name: '颜色',
					},
				],
				specChildList: [{
						id: 1,
						pid: 1,
						name: '普通版',
					},
					{
						id: 2,
						pid: 1,
						name: '豪华版',
					},
					{
						id: 3,
						pid: 1,
						name: 'M',
					},
					{
						id: 4,
						pid: 1,
						name: 'L',
					},
					{
						id: 5,
						pid: 1,
						name: 'XL',
					},
					{
						id: 6,
						pid: 1,
						name: 'XXL',
					},
					{
						id: 7,
						pid: 2,
						name: '白色',
					},
					{
						id: 8,
						pid: 2,
						name: '黑色',
					},
					{
						id: 9,
						pid: 2,
						name: '灰',
					},
				]
			};
		},
		onLoad(option) {
			this.contentid=option.id,
			this.id = option.id;
			this.goodslist(this.id)
			
			// this.reqUserinfo();	
		},
		methods: {
			gouwoche()
			{
				uni.showToast({
					title: "已加入购物车"
				})
				let userid = uniCloud.getCurrentUserInfo()
				db.collection('opendb-mall-goods').doc(this.contentid).get().then((res) => {
				 
					let list = {}
					let data = res.result.data[0]
					console.log(data)
				      list = {
						goods_id: data.category_id,
						// user_id: userid.uid,
						commodity_name: data.name,
						 shopping_date: new Date(),
						price: data.month_sell_count,
						commodity_quantity: 1,
						shopping_thumb: data.goods_thumb,
						specSelected: this.specSelected,
						// market_price: data._id.sku[0].market_price
					}
					db.collection('shoppingcar').add(list)
					console.log(list)
				})
			},
			onClickItem(e) {
				if (this.current != e.currentIndex) {
					this.current = e.currentIndex;
				}
			},
			
			goodslist(id) {
				db.collection('opendb-mall-goods').where({
						_id: id
					}).field("name,goods_thumb,goods_desc,goods_banner_imgs,price,total_sell_count,remain_count,keywords").get()
					.then(res => {
						console.log(res)
						this.goods = res.result.data[0];
						this.imgList = this.goods.goods_banner_imgs;
					})
			},
			addtocart() {
				let item = {};
				item = {
					"goodsid": this.id,
					"goodsname": this.goods.name,
					"price":this.goods.price,
					"goodsimg":this.goods.goods_thumb[0].fileID,
					"specSelected": this.specSelected
				}
				this.mycart.push(item)
			},
			//规格弹窗开关
			toggleSpec() {
				if (this.specClass === 'show') {
					this.specClass = 'hide';
					setTimeout(() => {
						this.specClass = 'none';
					}, 250);
				} else if (this.specClass === 'none') {
					this.specClass = 'show';
				}
			},
			//选择规格
			selectSpec(index, pid) {
				let list = this.specChildList;
				list.forEach(item => {
					if (item.pid === pid) {
						this.$set(item, 'selected', false);
					}
				})

				this.$set(list[index], 'selected', true);
				//存储已选择
				/**
				 * 修复选择规格存储错误
				 * 将这几行代码替换即可
				 * 选择的规格存放在specSelected中
				 */
				this.specSelected = [];
				list.forEach(item => {
					if (item.selected === true) {
						this.specSelected.push(item);
					}
				})
				// stopPrevent
				// stop
			}
		},

	}
</script>


<style>
	uni-button {
		font-size: 15px;
	}

	uni-image {
		width: 450px;
		height: 450px;
	}
</style>

<style lang='scss'>
	page {
		/* background: $page-color-base; */
		padding-bottom: 160rpx;
	}

	.icon-you {
		/* font-size: $font-base + 2rpx; */
		color: #888;
	}

	.carousel {
		height: 722rpx;
		position: relative;

		swiper {
			height: 100%;
		}

		.image-wrapper {
			width: 100%;
			height: 100%;
		}

		.swiper-item {
			display: flex;
			justify-content: center;
			align-content: center;
			height: 750rpx;
			overflow: hidden;

			image {
				width: 100%;
				height: 100%;
			}
		}

	}

	/* 标题简介 */
	.introduce-section {
		background: #fff;
		padding: 20rpx 30rpx;

		.title {
			font-size: 32rpx;
			/* color: $font-color-dark; */
			height: 50rpx;
			line-height: 50rpx;
		}

		.price-box {
			display: flex;
			align-items: baseline;
			height: 64rpx;
			padding: 10rpx 0;
			font-size: 26rpx;
			/* color: $uni-color-primary; */
		}

		.price {
			/* font-size: $font-lg + 2rpx; */
			font-size: 16px;
			color: red;
		}

		.m-price {
			margin: 0 12rpx;
			/* color: $font-color-light; */
			text-decoration: line-through;
		}

		.coupon-tip {
			align-items: center;
			padding: 4rpx 10rpx;
			/* background: $uni-color-primary; */
			/* font-size: $font-sm; */
			color: #fff;
			border-radius: 6rpx;
			line-height: 1;
			transform: translateY(-4rpx);
		}

		.bot-row {
			display: flex;
			align-items: center;
			height: 50rpx;
			font-size: 15px;
			color: #909399;

			text {
				flex: 1;
			}
		}
	}

	/* 分享 */
	.share-section {
		display: flex;
		align-items: center;
		/* color: $font-color-base; */
		color: #606266;
		/* background: linear-gradient(left, #fdf5f6, #fbebf6);
		 */
		background-color: #fdf5f6;
		padding: 12rpx 30rpx;

		.share-icon {
			display: flex;
			align-items: center;
			width: 70rpx;
			height: 30rpx;
			line-height: 1;
			border: 1px solid red;
			border-radius: 4rpx;
			position: relative;
			overflow: hidden;
			font-size: 22rpx;
			/* color: $uni-color-primary; */

			&:after {
				content: '';
				width: 50rpx;
				height: 50rpx;
				border-radius: 50%;
				left: -20rpx;
				top: -12rpx;
				position: absolute;
				/* background: $uni-color-primary; */
			}
		}

		.icon-xingxing {
			position: relative;
			z-index: 1;
			font-size: 24rpx;
			margin-left: 2rpx;
			margin-right: 10rpx;
			color: #fff;
			line-height: 1;
		}

		.tit {
			/* font-size: $font-base; */
			margin-left: 10rpx;
		}

		.icon-bangzhu1 {
			padding: 10rpx;
			font-size: 30rpx;
			line-height: 1;
		}

		.share-btn {
			flex: 1;
			text-align: right;
			font-size: 12px;
			color: #fa436a;
		}
	}

	.c-list {
		/* font-size: $font-sm + 2rpx; */
		/* color: $font-color-base; */
		line-height: 20px;
		font-size: 16px;
		color: #606266;
		background: #fff;

		.c-row {
			display: flex;
			align-items: center;
			padding: 20rpx 30rpx;
			position: relative;
		}

		.tit {
			width: 140rpx;
		}

		.con {
			flex: 1;
			/* color: $font-color-dark; */

			.selected-text {
				margin-right: 10rpx;
			}
		}

		.bz-list {
			height: 40rpx;
			/* font-size: $font-sm+2rpx; */
			/* color: $font-color-dark; */

			text {
				display: inline-block;
				margin-right: 30rpx;
			}
		}

		.con-list {
			flex: 1;
			display: flex;
			flex-direction: column;
			/* color: $font-color-dark; */
			line-height: 40rpx;
		}

		.red {
			/* color: $uni-color-primary; */
		}
	}

	/* 评价 */
	.eva-section {
		display: flex;
		flex-direction: column;
		padding: 20upx 30upx;
		background: #fff;
		margin-top: 16upx;
		
	
		.e-header {
			display: flex;
			align-items: center;
			height: 70upx;
			/* font-size: $font-sm + 2upx; */
			/* color: $font-color-light; */
	
			.tit {
				/* font-size: $font-base + 2upx; */
				/* color: $font-color-dark; */
				margin-right: 4upx;
				
			}
	
			.tip {
				flex: 1;
				text-align: right;
			}
	
			.icon-you {
				margin-left: 10upx;
			}
		}
	}
	
	.segmented-control {
		width: 100%
	}
	
	.eva-box {
		display: flex;
		padding: 20upx 0;
	
		.portrait {
			flex-shrink: 0;
			width: 80upx;
			height: 80upx;
			border-radius: 100px;
		}
	
		.right {
			flex: 1;
			display: flex;
			flex-direction: column;
			/* font-size: $font-base; */
			/* color: $font-color-base; */
			padding-left: 26upx;
	
			.con {
				/* font-size: $font-base; */
				/* color: $font-color-dark; */
				padding: 20upx 0;
			}
	
			.bot {
				display: flex;
				justify-content: space-between;
				/* font-size: $font-sm; */
				/* color: $font-color-light; */
			}
		}
	}
	

	/*  详情 */
	.detail-desc {
		background: #fff;
		margin-top: 16rpx;
		/* margin: 0 auto; */

		.d-header {
			display: flex;
			justify-content: center;
			align-items: center;
			height: 80rpx;
			/* font-size: $font-base + 2rpx; */
			/* color: $font-color-dark; */
			position: relative;

			text {
				padding: 0 20rpx;
				background: #fff;
				position: relative;
				z-index: 1;
			}

			&:after {
				position: absolute;
				left: 50%;
				top: 50%;
				transform: translateX(-50%);
				width: 300rpx;
				height: 0;
				content: '';
				border-bottom: 1px solid #ccc;
			}
		}
	}

	/* 规格选择弹窗 */
	.attr-content {
		padding: 10rpx 30rpx;

		.a-t {
			display: flex;

			image {
				width: 170rpx;
				height: 170rpx;
				flex-shrink: 0;
				margin-top: -40rpx;
				border-radius: 8rpx;
				;
			}

			.right {
				display: flex;
				flex-direction: column;
				padding-left: 24rpx;
				/* font-size: $font-sm + 2rpx; */
				/* color: $font-color-base; */
				line-height: 42rpx;

				.price {
					/* font-size: $font-lg; */
					/* color: $uni-color-primary; */
					margin-bottom: 10rpx;
				}

				.selected-text {
					margin-right: 10rpx;
				}
			}
		}

		.attr-list {
			display: flex;
			flex-direction: column;
			/* font-size: $font-base + 2rpx; */
			/* color: $font-color-base; */
			padding-top: 30rpx;
			padding-left: 10rpx;
		}

		.item-list {
			padding: 20rpx 0 0;
			display: flex;
			flex-wrap: wrap;

			text {
				display: flex;
				align-items: center;
				justify-content: center;
				background: #eee;
				margin-right: 20rpx;
				margin-bottom: 20rpx;
				border-radius: 100rpx;
				min-width: 60rpx;
				height: 60rpx;
				padding: 0 20rpx;
				/* font-size: $font-base; */
				/* color: $font-color-dark; */
			}

			.selected {
				background: #fbebee;
				/* color: $uni-color-primary; */
			}
		}
	}

	/*  弹出层 */
	.popup {
		position: fixed;
		left: 0;
		top: 0;
		right: 0;
		bottom: 0;
		z-index: 99;

		&.show {
			display: block;

			.mask {
				animation: showPopup 0.2s linear both;
			}

			.layer {
				animation: showLayer 0.2s linear both;
			}
		}

		&.hide {
			.mask {
				animation: hidePopup 0.2s linear both;
			}

			.layer {
				animation: hideLayer 0.2s linear both;
			}
		}

		&.none {
			display: none;
		}

		.mask {
			position: fixed;
			top: 0;
			width: 100%;
			height: 100%;
			z-index: 1;
			background-color: rgba(0, 0, 0, 0.4);
		}

		.layer {
			position: fixed;
			z-index: 99;
			bottom: 0;
			width: 100%;
			min-height: 40vh;
			border-radius: 10rpx 10rpx 0 0;
			background-color: #fff;

			.btn {
				height: 66rpx;
				line-height: 66rpx;
				border-radius: 100rpx;
				/* background: $uni-color-primary; */
				/* font-size: $font-base + 2rpx; */
				color: #000000;
				margin: 30rpx auto 20rpx;
			}
		}

		@keyframes showPopup {
			0% {
				opacity: 0;
			}

			100% {
				opacity: 1;
			}
		}

		@keyframes hidePopup {
			0% {
				opacity: 1;
			}

			100% {
				opacity: 0;
			}
		}

		@keyframes showLayer {
			0% {
				transform: translateY(120%);
			}

			100% {
				transform: translateY(0%);
			}
		}

		@keyframes hideLayer {
			0% {
				transform: translateY(0);
			}

			100% {
				transform: translateY(120%);
			}
		}
	}

	/* 底部操作菜单 */
	.page-bottom {
		position: fixed;
		left: 30rpx;
		bottom: 30rpx;
		z-index: 95;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 690rpx;
		height: 100rpx;
		background: rgba(255, 255, 255, .9);
		box-shadow: 0 0 20rpx 0 rgba(0, 0, 0, .5);
		border-radius: 16rpx;

		.p-b-btn {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			font-size: 26rpx;
			color: #606266;
			width: 96rpx;
			height: 80rpx;

			.yticon {
				font-size: 40rpx;
				line-height: 48rpx;
				/* color: $font-color-light; */
			}

			&.active,
			&.active .yticon {
				/* color: $uni-color-primary; */
			}

			.icon-fenxiang2 {
				font-size: 42rpx;
				transform: translateY(-2rpx);
			}

			.icon-shoucang {
				font-size: 46rpx;
			}
		}

		.action-btn-group {
			display: flex;
			height: 76rpx;
			border-radius: 100px;
			overflow: hidden;
			box-shadow: 0 20rpx 40rpx -16rpx #fa436a;
			box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
			background: linear-gradient(to right, #ffac30, #fa436a, #F56C6C);
			margin-left: 20rpx;
			position: relative;

			&:after {
				content: '';
				position: absolute;
				top: 50%;
				right: 50%;
				transform: translateY(-50%);
				height: 28rpx;
				width: 0;
				border-right: 1px solid rgba(255, 255, 255, .5);
			}

			.action-btn {
				display: flex;
				align-items: center;
				justify-content: center;
				width: 180rpx;
				height: 100%;
				/* font-size: $font-base; */
				padding: 0;
				border-radius: 0;
				background: transparent;
			}
		}
	}
</style>